package com.huizhonglm.diap.core.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.huizhonglm.diap.R;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-07-10 15:44
 */
public class TPWDUtil {

    private EditText mEditText;
    private LayoutInflater inflater;
    private View[] views;

    private Dialog dialog;
    private Context context;

    private StringBuilder builder;
    private OnInputCompleteListener mListener;

    public TPWDUtil(Context context, OnInputCompleteListener l) {
        this.context = context;
        this.mListener = l;
        inflater = LayoutInflater.from(context);
        builder = new StringBuilder();
        initWidget();
    }

    public void showDialog(){
        if (dialog != null)
            dialog.show();
    }

    private void initWidget() {
        View contentView = inflater.inflate(R.layout.dialog_input_tpwd, null);
        mEditText = (EditText) contentView.findViewById(R.id.input_edt);
        View point1 = contentView.findViewById(R.id.pwd_point_1);
        View point2 = contentView .findViewById(R.id.pwd_point_2);
        View point3 = contentView .findViewById(R.id.pwd_point_3);
        View point4 = contentView.findViewById(R.id.pwd_point_4);
        View point5 = contentView.findViewById(R.id.pwd_point_5);
        View point6 = contentView .findViewById(R.id.pwd_point_6);
        mEditText.addTextChangedListener(mTextWatcher);
        mEditText.setOnKeyListener(keyListener);
        views = new View[] { point1, point2, point3, point4, point5, point6 };
        dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                clear();
            }
        });
        Window window = dialog.getWindow();
        window.setContentView(contentView);
    }

    private TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() == 0) {
                return;
            }

            if (builder.length() < 6) {
                builder.append(s.toString());
                setTextValue();
            }
            s.delete(0, s.length());
        }

    };

    private View.OnKeyListener keyListener = new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_DEL
                    && event.getAction() == KeyEvent.ACTION_UP) {
                delTextValue();
                return true;
            }
            return false;
        }
    };

    private void setTextValue() {

        String str = builder.toString();
        int len = str.length();

        if (len <= 6) {
            views[len - 1].setVisibility(View.VISIBLE);
        }
        if (len == 6) {
            if (dialog != null) {
                dialog.dismiss();
            }
            if (mListener != null) {
                mListener.onInputCompleted(str);
            }
        }
    }

    private void delTextValue() {
        String str = builder.toString();
        int len = str.length();
        if (len == 0) {
            return;
        }
        if (len > 0 && len <= 6) {
            builder.delete(len - 1, len);
        }
        views[len - 1].setVisibility(View.INVISIBLE);
    }

    private void clear() {
        if (builder != null) {
            if (builder.length() == 6) {
                builder.delete(0, 6);
            }
        }
        if (views != null) {
            for (View poi : views) {
                poi.setVisibility(View.INVISIBLE);
            }
        }
    }

    public interface OnInputCompleteListener {
        void onInputCompleted(String tpwd);
    }
}
